package com.demo.steammanagement.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.steammanagement.common.QueryBuild;
import com.demo.steammanagement.common.ResultMsg;
import com.demo.steammanagement.config.response.BizException;
import com.demo.steammanagement.convert.GameAccountConvert;
import com.demo.steammanagement.convert.RentalsConvert;
import com.demo.steammanagement.model.entity.GameAccounts;
import com.demo.steammanagement.model.entity.Games;
import com.demo.steammanagement.model.entity.Rentals;
import com.demo.steammanagement.model.vo.GameAccountVO;
import com.demo.steammanagement.model.vo.RentalVO;
import com.demo.steammanagement.model.vo.Result;
import com.demo.steammanagement.service.IGameAccountsService;
import com.demo.steammanagement.service.IGamesService;
import com.demo.steammanagement.service.IRentalsService;
import com.demo.steammanagement.service.IUsersService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rentals")
public class RentalsController {
  @Autowired
  private IRentalsService rentalsService;
  @Autowired
  private IGameAccountsService gameAccountsService;
  @Autowired
  private IGamesService iGamesService;
  @Autowired
  private IUsersService usersService;

  @GetMapping("/page")
  public IPage<RentalVO> page(RentalVO vo) {

    if (ObjectUtils.isEmpty(vo)) {
      throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
    }

    Page<Rentals> page = new Page<>(vo.getPageNo(), vo.getPageSize());
    Page<Rentals> page1 = rentalsService.page(page, QueryBuild.rentalsQueryWrapper(vo));
    List<RentalVO>  rentalWithGameList =page1.getRecords().stream().map(item -> {
      RentalVO rentalVO = new RentalVO();
      BeanUtils.copyProperties(item, rentalVO);
      rentalVO.setUserAccount(usersService.getById(item.getUserId()).getAccount());
      return rentalVO;
    }).collect(Collectors.toList());
    IPage<RentalVO> resultPage = new Page<>();
    resultPage.setTotal(page1.getTotal());
    resultPage.setSize(page1.getSize());
    resultPage.setCurrent(page1.getCurrent());
    resultPage.setRecords(rentalWithGameList);
    return resultPage;

  }

  /**
   * 租用账号
   *
   * @param vo
   * @return
   */
  @PostMapping("/saveOrUpdate")
  @Transactional
  public Result saveOrUpdate(@RequestBody RentalVO vo) throws IOException {
    rentalsService.costomSaveOrUpdate(vo);
    GameAccounts gameAccounts = new GameAccounts();
    gameAccounts.setId(vo.getGameAccountId());
    gameAccounts.setStatus(1);
    gameAccountsService.updateById(gameAccounts);
    // 修改可用账号数
    Long count = gameAccountsService.count(new LambdaQueryWrapper<GameAccounts>()
            .eq(GameAccounts::getGameId, vo.getGameId())
            .eq(GameAccounts::getStatus, 0)
    );
    iGamesService.update(new LambdaUpdateWrapper<Games>()
            .set(Games::getAvailableAccounts, count)
            .eq(Games::getId, vo.getGameId()));
    return Result.success();
  }

  @GetMapping("/get")
  public Result get(RentalVO vo) {

    if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
      throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
    }
    return Result.success(rentalsService.getById(vo.getId()));

  }

  @GetMapping("/delete")
  public Result delete(RentalVO vo) {

    if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
      throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
    }
    return Result.success(rentalsService.removeById(vo.getId()));

  }
}
